package com.snopy.leetcode.index1_1000.index101_200;

import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.Set;

/**
 * @author snopy
 * @version 1.0.0
 * @classname Question160
 * @description 相交链表 easy
 *
 * @email 77912204@qq.com
 * @date 2022/05/15 19:10
 */
public class Question160 {
    public static void main(String[] args) {
        //new Question160().getIntersectionNode();
    }
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {

        Set<ListNode> set = new HashSet<>();
        while (headA!=null){
            set.add(headA);
            headA = headA.next;
        }
        while (headB!=null){
            if(!set.add(headB)){
                return headB;
            }
            headB = headB.next;
        }
        return null;
    }
    /**
     * @Description 双指针处理
     * @param headA:
     * @param headB:
     * @return: com.snopy.leetcode.index1_1000.index101_200.ListNode
     * @Date 2022-05-15
     * @Author txl77912204@gmail.com
     **/
    public ListNode getIntersectionNode2(ListNode headA, ListNode headB) {

        Set<ListNode> set = new HashSet<>();
        while (headA!=null){
            set.add(headA);
            headA = headA.next;
        }
        while (headB!=null){
            if(!set.add(headB)){
                return headB;
            }
            headB = headB.next;
        }
        return null;
    }
}
 class ListNode {
      int val;
      ListNode next;
      ListNode(int x) {
          val = x;
          next = null;
      }
}